<?php

namespace backend\controllers;
use yii\db\ActiveRecord;
use backend\models\Shop;
use backend\models\UploadFile;
use Yii;
use backend\models\Goods;
use backend\models\GoodsSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\data\ActiveDataProvider;
use yii\filters\AccessControl;

/**
 * GoodsController implements the CRUD actions for Goods model.
 */
class GoodsController extends Controller
{
    /**
     * {@inheritdoc}
     */

    /*
     * 商品数据接口*/
    public function actionGetgoods(){
        $request=Yii::$app->request;
        $limit=$request->get('limit');
        $page=$request->get('page');
        $offset=($page-1)*$limit;
        $data=Goods::getData($offset,$limit);
        //echo json_encode($data);die;
        $list['data']=$data;
        $list['count']=Goods::find()->where(['goods_state'=>0])->count();
        $list['code']=0;
        echo json_encode($list);
    }
    public function actionIndex(){
       return $this->render('show');
    }
    /*
     * 删除商品及列表*/
   public function actionDel(){
       $request=yii::$app->request;
       $id=$request->get('id');
       $res=Goods::findOne($id);
       $result=$res->delete();
       if($result){
           echo json_encode('1');
       }

   }

   /*
    * 修改商品*/
   public function actionUpdate(){

   }

   /*
    * 重新生成列表*/
   public function actionCreatelist(){
       $request=yii::$app->request;
       $id=$request->get('id');

       $idarray=explode(',',$id);
       array_pop($idarray);

       foreach ($idarray as $key=>$value) {
           //删除列表
           yii::$app->redis->lrem('goods' . $value, 0, 1);
           //商品库存
           $length = Goods::findOne($value)->goods_num;

           for($i=0;$i<$length;$i++){
               $b=yii::$app->redis->lpush('goods'.$value,'1');
           }
           //列表长度
           $len = yii::$app->redis->llen('goods' . $value);

       }
       echo json_encode($len);
   }
   /*添加订单并生成队列*/
   public function actionAdd(){
       $request=Yii::$app->request;
       $data=$request->post();
       $result=Goods::Addone($data);
       if($result){
           echo json_encode(['code'=>'200','msg'=>'添加成功']);
       }else
       {
           echo json_encode(['code'=>'40000','msg'=>'添加失败']);
       }
   }
   public function actionGoodadd(){
       return $this->render('goodsadd');
   }
    public function Upload(){

   }
}